package com.mr.orderclient.utils;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Class: HttpAspectUtil
 * User (作者):MRui
 * TODO (描述)：方法拦截工具类
 * Date 2018.09.22 11:52
 */
@Aspect
@Component
@Slf4j
public class HttpAspectUtil {

    @Pointcut("execution(public * com.mr.sell.controller.*.*(..))")
    public void pointuct(){

    }
    /**
    * User (作者): MRui
    * TODO (描述)：方法运行之前
    * Data：2018.09.22 11:59
    */
    @Before("pointuct()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        /**url*/
        log.info("url={}",request.getRequestURI());
        /**method*/
        log.info("method={}",request.getMethod());
        /**ip*/
        log.info("ip={}",request.getRemoteAddr());
        /**类方法*/
        log.info("classMethod={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        /**参数*/
        log.info("args={}",joinPoint.getArgs());
    }
    /**
    * User (作者): MRui
    * TODO (描述)方法运行之后
    * Data：2018.09.22 11:58
    */
    @After("pointuct()")
    public void doAfter(){
        log.info("请求完毕！！！");
    }
    /**
    * User (作者): MRui
    * TODO (描述)：返回的内容
    * Data：2018.09.22 12:13
    */
    @AfterReturning(returning = "object",pointcut = "pointuct()")
    public void doAfterReturning(Object object){
        log.info("response={}",object.toString());
    }
}
